﻿/*=======================================================================
  (c) Topbot. For Good Developers only.
=======================================================================*/

namespace BlogsAPI.Rdf
{
    using RssToolkit.Xml;
    using System;
    using System.Xml;
    using System.Xml.Serialization;

    /// <summary>
    /// RdfDocument
    /// </summary>
    [Serializable]
    [XmlRoot("RDF",Namespace = "http://www.w3.org/1999/02/22-rdf-syntax-ns#")]
    public class RdfDocument : XmlDocumentBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RdfDocument"/> class.
        /// </summary>
        public RdfDocument()
        {
        }

        #region "Properties"

        /// <summary>
        /// Gets or sets the language.
        /// </summary>
        /// <value>The language.</value>
        [XmlAttribute("lang", Namespace = "http://www.w3.org/XML/1998/namespace")]
        public string Lang { get; set; }

        /// <summary>
        /// Gets or sets the main object of FOAF.
        /// </summary>
        /// <value>The FoafAgent.</value>
        [XmlElement("Person", Namespace = "http://xmlns.com/foaf/0.1/", Type = typeof(FoafPerson))]
        [XmlElement("Group", Namespace = "http://xmlns.com/foaf/0.1/", Type = typeof(FoafGroup))]
        [XmlElement("Organization", Namespace = "http://xmlns.com/foaf/0.1/", Type = typeof(FoafOrganization))]
        [XmlElement("Syndicated", Namespace = "http://blogs.yandex.ru/schema/foaf/", Type = typeof(YaSyndicated))]
        public FoafAgent Who { get; set; }

        /// <summary>
        /// The foaf:page property relates a thing to a document about that thing.
        /// </summary>
        /// <value>A page or document about this thing.</value>
        /// <remarks>Copy of //foaf:page/foaf:Document[0]</remarks>
        [XmlElement("Document", Namespace = "http://xmlns.com/foaf/0.1/")]
        public RdfElement Document { 
            get
            {
                if (Who != null && Who.Pages != null && Who.Pages.Length > 0)
                {
                    return Who.Pages[0];
                }
                return null;
            }
            set
            {
                if(Who == null) Who = new FoafAgent();
                if(Who.Pages==null || Who.Pages.Length==0)
                {
                    Who.Pages = new[] {value};
                }
                else
                {
                    Who.Pages[0] = value;
                }
            }
        }

        #endregion

        /// <summary>
        /// Loads from XML.
        /// </summary>
        /// <param name="xml">The XML.</param>
        /// <returns>RssDocument</returns>
        public static RdfDocument Load(string xml)
        {
            return Load<RdfDocument>(xml);
        }

        /// <summary>
        /// Loads the specified URL.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns>RdfDocument</returns>
        public static RdfDocument Load(Uri url)
        {
            return Load<RdfDocument>(url, true);
        }

        /// <summary>
        /// Loads the specified URL.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="useCache">Save loaded data in cache</param>
        /// <returns>RdfDocument</returns>
        public static RdfDocument Load(Uri url, Boolean useCache)
        {
            return Load<RdfDocument>(url, useCache);
        }

        /// <summary>
        /// Loads the specified reader.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns>RssDocument</returns>
        public static RdfDocument Load(XmlReader reader)
        {
            return Load<RdfDocument>(reader);
        }

        /// <summary>
        /// Returns Xml in the type specified by outputType
        /// </summary>
        /// <param name="outputType">Type of the output.</param>
        /// <returns></returns>
        public override string ToXml(DocumentType outputType)
        {
            return ToXml(outputType, this);
        }
    }

}
